﻿using FreeDream.Entity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.DBUtility
{
    public partial class EFCoreDBFirstContext : DbContext
    {
        #region 说明

        //Install-package Microsoft.EntityFrameworkCore
        //Install-package Microsoft.EntityFrameworkCore.Tools
        //Install-package Microsoft.EntityFrameworkCore.Design
        //Install-package Microsoft.EntityFrameworkCore.SqlServer
        //Install-package Microsoft.EntityFrameworkCore.SqlServer.Design

        //scaffold-dbcontext "server=.;database=EFCoreDBFirst;uid=sa;pwd=123;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
        //-OutputDir "Models" 可不写，EFCore中只能通过命令生成了，不能像以前EF6一样可以鼠标操作生成了，如果加上了则会在项目中添加models文件夹，生成的context和实体类都放到models文件夹中，如不加，实体则直接生成到项目中

        #endregion
        /// <summary>
        /// DBFirst
        /// scaffold-dbcontext "server=.;database=EFCoreDBFirst;uid=sa;pwd=123;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
        /// </summary>
        public EFCoreDBFirstContext()
        {
        }

        public EFCoreDBFirstContext(DbContextOptions<EFCoreDBFirstContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Post> Posts { get; set; } = null!;
        public virtual DbSet<User> Users { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                //#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("server=.;database=EFCoreDBFirst;uid=sa;pwd=123;");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Post>(entity =>
            {
                entity.ToTable("Post");

                entity.Property(e => e.Id)
                    .HasMaxLength(36)
                    .IsUnicode(false);

                entity.Property(e => e.Content).HasMaxLength(500);

                entity.Property(e => e.CreateDateTime).HasColumnType("datetime");

                entity.Property(e => e.Title).HasMaxLength(50);
            });

            modelBuilder.Entity<User>(entity =>
            {
                entity.ToTable("User");

                entity.Property(e => e.Id)
                    .HasMaxLength(36)
                    .IsUnicode(false);

                entity.Property(e => e.Code).HasMaxLength(50);

                entity.Property(e => e.CreateDateTime).HasColumnType("datetime");

                entity.Property(e => e.Name).HasMaxLength(50);
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }

    #region 实体

    //public partial class Post
    //{
    //    public string Id { get; set; } = null!;
    //    public string? Title { get; set; }
    //    public string? Content { get; set; }
    //    public DateTime? CreateDateTime { get; set; }
    //}

    //public partial class User
    //{
    //    public string Id { get; set; } = null!;
    //    public string? Code { get; set; }
    //    public string? Name { get; set; }
    //    public DateTime? CreateDateTime { get; set; }
    //}

    #endregion
}
